#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/4/20 14:13
# @Author : 闫峰
# @File    : bundle_deal.py
import time

import requests

from third_services.shopee.base_tool import shop_auth, db, shop_auth_shop
from third_services.shopee.common.common_func import get_timestamp, get_access_token, get_shop_id

from utils.otherutils.json_format import json_format


class InterfaceBundleDeal:
    base_url = "https://partner.shopeemobile.com"
    partner_id = 2001656

    def get_bundle_deal_list(self, ordersourceid, page_no,
                             apiService="/api/v2/bundle_deal/get_bundle_deal_list"):
        """使用此api获取一个商店折扣活动的详细信息"""
        url = self.base_url + apiService
        payload = {}
        headers = {}

        param = {
            "partner_id": self.partner_id,
            "timestamp": get_timestamp(),
            "access_token": get_access_token(ordersourceid),
            "sign": shop_auth_shop(ordersourceid, apiService),
            "shop_id": get_shop_id(ordersourceid),
            "time_status": 2,
            "page_size": 100,
            "page_no": page_no
        }
        response = requests.request("GET", url, headers=headers, data=payload, params=param,
                                    allow_redirects=False).json()

        while True:
            if response['error'] == 'error_sign':
                param = {
                    "partner_id": self.partner_id,
                    "timestamp": get_timestamp(),
                    "access_token": get_access_token(ordersourceid),
                    "sign": shop_auth_shop(ordersourceid, apiService),
                    "shop_id": get_shop_id(ordersourceid),
                    "time_status": 1,
                    "page_size": 100,
                    "page_no": page_no

                }
                response = requests.request("GET", url, headers=headers, data=payload, params=param,
                                            allow_redirects=False).json()
            else:
                break

        return response

    def get_bundle_deal(self, ordersourceid, bundle_deal_id,
                        apiService="/api/v2/bundle_deal/get_bundle_deal"):
        """使用此api获取一个商店折扣活动的详细信息"""
        url = self.base_url + apiService
        payload = {}
        headers = {}

        param = {
            "partner_id": self.partner_id,
            "timestamp": get_timestamp(),
            "access_token": get_access_token(ordersourceid),
            "sign": shop_auth_shop(ordersourceid, apiService),
            "shop_id": get_shop_id(ordersourceid),
            "bundle_deal_id": bundle_deal_id

        }
        response = requests.request("GET", url, headers=headers, data=payload, params=param,
                                    allow_redirects=False).json()

        while True:
            if response['error'] == 'error_sign':
                param = {
                    "partner_id": self.partner_id,
                    "timestamp": get_timestamp(),
                    "access_token": get_access_token(ordersourceid),
                    "sign": shop_auth_shop(ordersourceid, apiService),
                    "shop_id": get_shop_id(ordersourceid),
                    "bundle_deal_id": bundle_deal_id

                }
                response = requests.request("GET", url, headers=headers, data=payload, params=param,
                                            allow_redirects=False).json()
            else:
                break

        return response

    def get_bundle_deal_item(self, ordersourceid, bundle_deal_id,
                             apiService="/api/v2/bundle_deal/get_bundle_deal_item"):
        """使用此api获取一个商店折扣活动的详细信息"""
        url = self.base_url + apiService
        payload = {}
        headers = {}

        param = {
            "partner_id": self.partner_id,
            "timestamp": get_timestamp(),
            "access_token": get_access_token(ordersourceid),
            "sign": shop_auth_shop(ordersourceid, apiService),
            "shop_id": get_shop_id(ordersourceid),
            "bundle_deal_id": bundle_deal_id

        }
        response = requests.request("GET", url, headers=headers, data=payload, params=param,
                                    allow_redirects=False).json()

        while True:
            if response['error'] == 'error_sign':
                param = {
                    "partner_id": self.partner_id,
                    "timestamp": get_timestamp(),
                    "access_token": get_access_token(ordersourceid),
                    "sign": shop_auth_shop(ordersourceid, apiService),
                    "shop_id": get_shop_id(ordersourceid),
                    "bundle_deal_id": bundle_deal_id

                }
                response = requests.request("GET", url, headers=headers, data=payload, params=param,
                                            allow_redirects=False).json()
            else:
                break

        return response


    def delete_bundle_deal_item(self, ordersourceid,
                                apiService="/api/v2/bundle_deal/delete_bundle_deal_item"):
        """使用此api获取一个商店折扣活动的详细信息"""
        url = self.base_url + apiService
        payload = {
            "bundle_deal_id": 209084000,
            "item_list": [
                {
                    "item_id": 19388478049,
                    "status": 0
                }
            ]
        }
        headers = {}

        param = {
            "partner_id": self.partner_id,
            "timestamp": get_timestamp(),
            "access_token": get_access_token(ordersourceid),
            "sign": shop_auth_shop(ordersourceid, apiService),
            "shop_id": get_shop_id(ordersourceid)
        }
        response = requests.request("POST", url, headers=headers, json=payload, params=param,
                                    allow_redirects=False).json()
        return response

    def add_bundle_deal_item(self, ordersourceid,
                                apiService="/api/v2/bundle_deal/add_bundle_deal_item"):
        """使用此api获取一个商店折扣活动的详细信息"""
        url = self.base_url + apiService
        payload = {
            "bundle_deal_id": 209084000,
            "item_list": [
                {
                    "item_id": 19388478049,
                    "status": 0
                }
            ]
        }
        headers = {}

        param = {
            "partner_id": self.partner_id,
            "timestamp": get_timestamp(),
            "access_token": get_access_token(ordersourceid),
            "sign": shop_auth_shop(ordersourceid, apiService),
            "shop_id": get_shop_id(ordersourceid)
        }
        response = requests.request("POST", url, headers=headers, json=payload, params=param,
                                    allow_redirects=False).json()
        return response

if __name__ == '__main__':
    # print(InterfaceBundleDeal().get_bundle_deal_list(13531,1))
    res = InterfaceBundleDeal().get_bundle_deal(19100,208915682)
    print(res)
    # print(InterfaceBundleDeal().get_bundle_deal_item(13530,208339592))
    # print(InterfaceBundleDeal().add_bundle_deal_item(13531))
    # print(InterfaceBundleDeal().get_bundle_deal(13673, 200767630))
